package ca.uoit.dblp;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class AuthorGraph {
	public static class Entry {
		public int count;
		public double d0;
		public Entry(int count) {
			this(count, 0);
		}
		public Entry(int count, double d0) {
			this.count = count;
			this.d0 = d0;
		}
	}
	public static interface EdgeFunc {
		public void call(long i1, long i2, Entry e);
	}
	
	public static interface NeighbourhoodFunc {
		public void call(long i1, Map<Long, Entry> n);
	}
	
	public Map<String, Long> V;
	public Map<Long, Map<Long, Entry>> E;
	public int nodeCount = 0;
	
	public AuthorGraph(String inputFile) {
		this.V = new HashMap<String, Long>();
		this.E = new HashMap<Long, Map<Long, Entry>>();
	}
	
	public AuthorGraph load(String inputFile) throws IOException {
		String line;
		BufferedReader reader = new BufferedReader(new FileReader(inputFile));
		while((line = reader.readLine()) != null) {
			String[] contents = line.split(",");
			long id1 = this.getNodeID(contents[0]);
			long id2 = this.getNodeID(contents[1]);
			int count = Integer.parseInt(contents[2]);
			Map<Long, Entry> neighbourhood = this.E.get(id1);
			Entry e = new Entry(count);
			assert(e != null);
			if(neighbourhood == null) {
				neighbourhood = new HashMap<Long, Entry>();
				this.E.put(id1, neighbourhood);
			}
			neighbourhood.put(id2, e);
		}
		return this;
	}
	public long getNodeID(String name) {
		if(this.V.containsKey(name)) {
			return this.V.get(name);
		} else {
			long id = this.getNewID();
			this.V.put(name, id);
			return id;
		}
	}
	public synchronized long getNewID() {
		this.nodeCount ++;
		return this.nodeCount;
	}
	
}
